package com.ruoyi.classai.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.classai.domain.EwcCommonModule;
import com.ruoyi.classai.domain.dto.ModuleLessonPreparationModificationDto;
import com.ruoyi.classai.domain.dto.PublicIdQueryInterfaceDto;
import com.ruoyi.classai.service.IEwcCommonModuleService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.ruoyi.classai.mapper.EwcChapterTeachingResourcesMapper;
import com.ruoyi.classai.domain.EwcChapterTeachingResources;
import com.ruoyi.classai.service.IEwcChapterTeachingResourcesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.UUID;

/**
 * 章节教学资源Service业务层处理
 * 
 * @author lcj
 * @date 2025-02-28
 */
@Service
public class EwcChapterTeachingResourcesServiceImpl extends ServiceImpl<EwcChapterTeachingResourcesMapper, EwcChapterTeachingResources> implements IEwcChapterTeachingResourcesService {

@Autowired
private IEwcCommonModuleService ewcCommonModuleService;
    @Override
    public AjaxResult saveOne(PublicIdQueryInterfaceDto publicIdQueryInterfaceDto) {
        ewcChapterTeachingResources.setChapterUniqueId(publicIdQueryInterfaceDto.getChapterUniqueId());
        ewcChapterTeachingResources.setModuleCode(publicIdQueryInterfaceDto.getModuleCode());
        ewcChapterTeachingResources.setClassId(publicIdQueryInterfaceDto.getClassId());
        ewcChapterTeachingResources.setCourseId(publicIdQueryInterfaceDto.getCourseId());
        ewcChapterTeachingResources.setTeacherId(ThirdSessionHolder.getThirdSession().getId());
//        ewcChapterTeachingResources.setModuleState("1");
        UUID uuid = UUID.randomUUID();
        String uuidString = uuid.toString();
        ewcChapterTeachingResources.setResourceId(uuidString);
       try {
            // 调用 MyBatis-Plus 提供的插入方法
            boolean result = save(ewcChapterTeachingResources);
            if (result) {
                return AjaxResult.success("数据插入成功");
            } else {
                return AjaxResult.error("数据插入失败");
            }
        } catch (DuplicateKeyException e) {
            return AjaxResult.error("数据已存在");
        }
    }

    @Override
    public boolean deleteOne(PublicIdQueryInterfaceDto publicIdQueryInterfaceDto) {
        // 创建 QueryWrapper 对象
        QueryWrapper<EwcChapterTeachingResources> queryWrapper = new QueryWrapper<>();
        // 根据实体属性设置删除条件
        queryWrapper.eq("chapter_unique_id", publicIdQueryInterfaceDto.getChapterUniqueId());
        queryWrapper.eq("module_code", publicIdQueryInterfaceDto.getModuleCode());
        queryWrapper.eq("class_id", publicIdQueryInterfaceDto.getClassId());
        queryWrapper.eq("course_id", publicIdQueryInterfaceDto.getCourseId());
        queryWrapper.eq("teacher_id", ThirdSessionHolder.getThirdSession().getId());
        // 执行删除操作
        int rows = this.baseMapper.delete(queryWrapper);
        return rows > 0;
    }

    @Override
    public boolean updateState(PublicIdQueryInterfaceDto publicIdQueryInterfaceDto) {
        // 创建 UpdateWrapper 对象
        UpdateWrapper<EwcChapterTeachingResources> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("chapter_unique_id", publicIdQueryInterfaceDto.getChapterUniqueId())
                .eq("teacher_id", ThirdSessionHolder.getThirdSession().getId())
                .eq("course_id", publicIdQueryInterfaceDto.getCourseId())
                .eq("class_id", publicIdQueryInterfaceDto.getClassId());
        // 设置要更新的字段及其值
        EwcChapterTeachingResources entity = new EwcChapterTeachingResources();
//        entity.setModuleState("2");
        // 执行更新操作
        return this.update(entity, updateWrapper);
    }

    @Override
    public boolean updateResources(ModuleLessonPreparationModificationDto moduleLessonPreparationModificationDto) {
        // 创建 UpdateWrapper 对象
        UpdateWrapper<EwcChapterTeachingResources> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("resource_id", moduleLessonPreparationModificationDto.getResourceId());
        // 设置要更新的字段及其值
        EwcChapterTeachingResources entity = new EwcChapterTeachingResources();
//        entity.setCoursewareMaterials(moduleLessonPreparationModificationDto.getCoursewareMaterials());
        entity.setCoursewareMaterialsDescription(moduleLessonPreparationModificationDto.getCoursewareMaterialsDescription());
//        entity.setLearningSituationAnalysis(moduleLessonPreparationModificationDto.getLearningSituationAnalysis());
//        entity.setAttendanceModule(moduleLessonPreparationModificationDto.getAttendanceModule());
        entity.setIntelligentAttendance(moduleLessonPreparationModificationDto.getIntelligentAttendance());
//        entity.setCommentModule(moduleLessonPreparationModificationDto.getCommentModule());
        entity.setGroupComment(moduleLessonPreparationModificationDto.getGroupComment());
        entity.setState("1");
        // 执行更新操作
        return this.update(entity, updateWrapper);
    }

    @Override
    public boolean clearResources(ModuleLessonPreparationModificationDto moduleLessonPreparationModificationDto) {
        // 创建 UpdateWrapper 对象
        UpdateWrapper<EwcChapterTeachingResources> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("resource_id",moduleLessonPreparationModificationDto.getResourceId());
        // 设置要更新的字段及其值
//        EwcChapterTeachingResources entity = new EwcChapterTeachingResources();
//        entity.setCoursewareMaterialsTemp("");
//        entity.setCoursewareMaterialsDescriptionTemp("");
        // 执行更新操作
//        return this.update(entity, updateWrapper);
        return this.remove(updateWrapper);
    }

    @Override
    public AjaxResult addResources(ModuleLessonPreparationModificationDto moduleLessonPreparationModificationDto) {


//        ewcChapterTeachingResources.setCoursewareMaterials(moduleLessonPreparationModificationDto.getCoursewareMaterials());
        ewcChapterTeachingResources.setCoursewareMaterialsDescription(moduleLessonPreparationModificationDto.getCoursewareMaterialsDescription());
        ewcChapterTeachingResources.setState("2");
        ewcChapterTeachingResources.setIntelligentAttendance(moduleLessonPreparationModificationDto.getIntelligentAttendance());
//            ewcChapterTeachingResources.setCommentModule(moduleLessonPreparationModificationDto.getCommentModule());
        ewcChapterTeachingResources.setGroupComment(moduleLessonPreparationModificationDto.getGroupComment());
        ewcChapterTeachingResources.setLearningSituationAnalysisChapter(moduleLessonPreparationModificationDto.getSubmitChapterUniqueId());

        QueryWrapper<EwcCommonModule> queryWrapperModule = new QueryWrapper<>();
        queryWrapperModule.eq("module_code",moduleLessonPreparationModificationDto.getModuleCode());
        EwcCommonModule onee = ewcCommonModuleService.getOne(queryWrapperModule);
        ewcChapterTeachingResources.setType(onee.getType());
//        上传文件 该模块上传文件为空 则更新

        //备课课后作业信息补充
        ewcChapterTeachingResources.setHomeworkModule(moduleLessonPreparationModificationDto.getHomeworkModule());
        ewcChapterTeachingResources.setHomeworkType(moduleLessonPreparationModificationDto.getHomeworkType());
        ewcChapterTeachingResources.setHomeworkTitle(moduleLessonPreparationModificationDto.getHomeworkTitle());
        ewcChapterTeachingResources.setHomeworkContent(moduleLessonPreparationModificationDto.getHomeworkContent());
        ewcChapterTeachingResources.setHomeworkDeadline(moduleLessonPreparationModificationDto.getHomeworkDeadline());
        ewcChapterTeachingResources.setHomeworkFilepath(moduleLessonPreparationModificationDto.getHomeworkFilepath());
        //慧评助手信息补充
        ewcChapterTeachingResources.setHelperVideoType(moduleLessonPreparationModificationDto.getHelperVideoType());
        ewcChapterTeachingResources.setHelperFilepath(moduleLessonPreparationModificationDto.getHelperFilepath());
        ewcChapterTeachingResources.setRepositoryType(moduleLessonPreparationModificationDto.getRepositoryType());
        //学情分析信息补充
        ewcChapterTeachingResources.setStudySuggestion(moduleLessonPreparationModificationDto.getStudySuggestion());
        ewcChapterTeachingResources.setGroupSuggestion(moduleLessonPreparationModificationDto.getGroupSuggestion());

        QueryWrapper<EwcChapterTeachingResources> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_unique_id",moduleLessonPreparationModificationDto.getChapterUniqueId());
        queryWrapper.eq("module_code",moduleLessonPreparationModificationDto.getModuleCode());
        queryWrapper.eq("teacher_id",ThirdSessionHolder.getThirdSession().getId());
        queryWrapper.eq("class_id",moduleLessonPreparationModificationDto.getClassId());
        queryWrapper.eq("course_id",moduleLessonPreparationModificationDto.getCourseId());
        long moduleCodeExist = this.count(queryWrapper);

//        queryWrapper.eq("courseware_materials","");
//        List<EwcChapterTeachingResources> ewcChapterTeachingResourcesCount = this.list(queryWrapper);
        if(moduleCodeExist>0){
            ewcChapterTeachingResources.setCourseId(null);
            ewcChapterTeachingResources.setResourceId(null);
            ewcChapterTeachingResources.setChapterUniqueId(null);
            ewcChapterTeachingResources.setModuleCode(null);
            ewcChapterTeachingResources.setTeacherId(null);
            ewcChapterTeachingResources.setClassId(null);
            boolean update = this.update(ewcChapterTeachingResources, queryWrapper);
            if (update){
                return AjaxResult.success("更新成功");
            }else{
                return AjaxResult.error("更新失败");
            }
        }else {
            ewcChapterTeachingResources.setChapterUniqueId(moduleLessonPreparationModificationDto.getChapterUniqueId());
            ewcChapterTeachingResources.setModuleCode(moduleLessonPreparationModificationDto.getModuleCode());
            ewcChapterTeachingResources.setTeacherId(ThirdSessionHolder.getThirdSession().getId());
            ewcChapterTeachingResources.setClassId(moduleLessonPreparationModificationDto.getClassId());
            ewcChapterTeachingResources.setCourseId(moduleLessonPreparationModificationDto.getCourseId());
//            ewcChapterTeachingResources.setLearningSituationAnalysis(moduleLessonPreparationModificationDto.getLearningSituationAnalysis());
//            ewcChapterTeachingResources.setAttendanceModule(moduleLessonPreparationModificationDto.getAttendanceModule());
            UUID uuid = UUID.randomUUID();
            String uuidString = uuid.toString();
            ewcChapterTeachingResources.setResourceId(uuidString);
            boolean result = this.save(ewcChapterTeachingResources);

            if (result) {
                return AjaxResult.success(ewcChapterTeachingResources);
            } else {
                return AjaxResult.error("数据插入失败");
            }
        }

    }

    @Override
    public boolean submitModule(ModuleLessonPreparationModificationDto moduleLessonPreparationModificationDto) {
        // 创建 UpdateWrapper 对象
        UpdateWrapper<EwcChapterTeachingResources> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("chapter_unique_id", moduleLessonPreparationModificationDto.getChapterUniqueId());
        updateWrapper.eq("module_code", moduleLessonPreparationModificationDto.getModuleCode());
        updateWrapper.eq("class_id", moduleLessonPreparationModificationDto.getClassId());
        updateWrapper.eq("course_id", moduleLessonPreparationModificationDto.getCourseId());
        updateWrapper.eq("teacher_id", ThirdSessionHolder.getThirdSession().getId());
//        updateWrapper.setSql("courseware_materials = courseware_materials_temp");
//        updateWrapper.setSql("courseware_materials_description = courseware_materials_description_temp");
//        updateWrapper.setSql("intelligent_attendance = intelligent_attendance_temp");
//        updateWrapper.setSql("group_comment = group_comment_temp");
//        updateWrapper.setSql("courseware_materials_temp =null");
//        updateWrapper.setSql("courseware_materials_description_temp =null");
//        updateWrapper.setSql("intelligent_attendance_temp = null");
//        updateWrapper.setSql("group_comment_temp = null");
        updateWrapper.setSql("state = '2'");

        // 设置要更新的字段及其值
        EwcChapterTeachingResources entity = new EwcChapterTeachingResources();
        // 执行更新操作
        return this.update(entity, updateWrapper);

    }

    @Override
    public int submitModuleDelet(ModuleLessonPreparationModificationDto moduleLessonPreparationModificationDto) {
        // 创建 UpdateWrapper 对象
        QueryWrapper<EwcChapterTeachingResources> queryWrapper  = new QueryWrapper<>();
        // 设置更新条件
        queryWrapper .eq("chapter_unique_id", moduleLessonPreparationModificationDto.getChapterUniqueId());
        queryWrapper .eq("module_code", moduleLessonPreparationModificationDto.getModuleCode());
        queryWrapper .eq("class_id", moduleLessonPreparationModificationDto.getClassId());
        queryWrapper .eq("course_id", moduleLessonPreparationModificationDto.getCourseId());
        queryWrapper .eq("teacher_id", ThirdSessionHolder.getThirdSession().getId());

        queryWrapper.isNull("courseware_materials");
//        queryWrapper.isNull("courseware_materials_temp");
        queryWrapper.isNull("courseware_materials_description");
        queryWrapper.isNull("learning_situation_analysis");
        queryWrapper.isNull("attendance_module");
        queryWrapper.isNull("intelligent_attendance");
        queryWrapper.isNull("comment_module");
        queryWrapper.isNull("group_comment");
        // 执行更新操作
        return this.baseMapper.delete(queryWrapper);
    }

    @Override
    public boolean submitAnalysis(ModuleLessonPreparationModificationDto moduleLessonPreparationModificationDto) {
        UpdateWrapper<EwcChapterTeachingResources> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("resource_id", moduleLessonPreparationModificationDto.getResourceId());
        updateWrapper.eq("teacher_id", ThirdSessionHolder.getThirdSession().getId());

        // 设置要更新的字段及其值learning_situation_analysis_chapter
        EwcChapterTeachingResources entity = new EwcChapterTeachingResources();
        entity.setLearningSituationAnalysisChapter(moduleLessonPreparationModificationDto.getSubmitChapterUniqueId());
        // 执行更新操作
        return this.update(entity, updateWrapper);
    }

    @Override
    public boolean learningSituationAnalysisSubmit(ModuleLessonPreparationModificationDto moduleLessonPreparationModificationDto) {
        UpdateWrapper<EwcChapterTeachingResources> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("module_code", moduleLessonPreparationModificationDto.getModuleCode());
        updateWrapper.eq("teacher_id", ThirdSessionHolder.getThirdSession().getId());
        updateWrapper.eq("course_id", moduleLessonPreparationModificationDto.getCourseId());
        updateWrapper.eq("chapter_unique_id", moduleLessonPreparationModificationDto.getChapterUniqueId());


        // 设置要更新的字段及其值learning_situation_analysis_chapter
        EwcChapterTeachingResources entity = new EwcChapterTeachingResources();
        entity.setState("2");
        // 执行更新操作
        return this.update(entity, updateWrapper);
    }
}
